package xj.graph2d.command;

import xj.graph2d.SimpleDrawDocument;
import xj.graph2d.SimpleDrawView;
import xj.graph2d.TextShape;

public class TextEditCommand 
  extends DeltaCommand {

  public TextEditCommand(SimpleDrawView view, 
			 SimpleDrawDocument doc,
			 TextShape shape) {
    super(view, doc, shape);
    this.text = shape;
    if (shape != null) {
      start = shape.getString();
    }
  }

  @Override
  protected void forward() {
    text.setString(end);
  }

  @Override
  protected void backward() {
    if (!isCommandSet) {
      setCommand();
    }
    text.setString(start);
  }

  @Override
  public boolean isSignificant() {
    if (!isCommandSet) {
      setCommand();
    }
    if (start == null) {
      return (end != null);
    } else {
      return (!start.equals(end));
    }
  }

  public void setCommand() {
    if (!isCommandSet) {
      if (end == null) {
	end = text.getString();
      }
      isCommandSet = true;
    }
  }

  @Override
  public String toString() {
    String s = "";
    s = (text.getName() + " \"" + text.getString() + "\"");
    s += (" start=" + (start != null ? ("\"" +start + "\"") : ""));
    s += (" end=" + (end != null ? ("\"" + end + "\"") : ""));
    return (getClass().getName() + ": " + s);
  }

  protected TextShape text;

  protected String start = null;

  protected String end = null;

}
